package com.code.thread.abc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class AbcLockSupport {
    static  Thread t1,t2,t3;

    public static void main(String[] args) {
        t1 = new Thread(()->{
            while (true) {

                // 暂停当前线程
                LockSupport.park();
                System.out.println("A");
                // 恢复某个线程的运行
                LockSupport.unpark(t2);
            }

        });

        t2 = new Thread(()->{
            while (true) {
                LockSupport.park();
                System.out.println("B");
                LockSupport.unpark(t3);
            }

        });

        t3 = new Thread(()->{
            while (true) {
                // 如果执行时先unpark(t1)，t1再park，会存在问题。
                LockSupport.unpark(t1);
                LockSupport.park();
                System.out.println("C");
            }

        });



        t1.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t2.start();
        t3.start();

    }
}
